package com.gxuwz.prrs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.gxuwz.prrs.R;
import com.gxuwz.prrs.bean.PianoRoom;
import com.gxuwz.prrs.bean.PianoRoomAppointment;
import com.gxuwz.prrs.service.impl.PianoRoomServiceImpl;
import com.gxuwz.prrs.service.ServiceHolder;
import com.gxuwz.prrs.utils.NavigationUtils;

import java.util.List;

/**
 * @author 张钧
 * @Description
 * @create 2023-05-17 下午 09:46
 */

public class PianoRoomAdapter extends UpdatableAbstractAdapter<PianoRoom> {
	private final List<PianoRoom> pianoRooms;
	private final LayoutInflater inflater;

	PianoRoomServiceImpl pianoRoomServiceImpl = ServiceHolder.getInstance().getBean(PianoRoomServiceImpl.class);

	public PianoRoomAdapter(Context context, List<PianoRoom> pianoRooms) {
		this.pianoRooms = pianoRooms;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return pianoRooms.size();
	}

	@Override
	public PianoRoom getItem(int i) {
		return pianoRooms.get(i);
	}

	@Override
	public long getItemId(int i) {
		return pianoRooms.get(i).getPianoRoomId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		PianoViewHolder pianoViewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.piano_room_item_layout, parent, false);
			pianoViewHolder = new PianoViewHolder();
			pianoViewHolder.setContent(convertView.findViewById(R.id.piano_no));
			pianoViewHolder.setTimeSpinner(convertView.findViewById(R.id.time_spinner));
			pianoViewHolder.setAppointButton(convertView.findViewById(R.id.appointment_button));
			convertView.setTag(pianoViewHolder);
		} else {
			pianoViewHolder = (PianoViewHolder) convertView.getTag();
		}
		PianoRoom itemData = getItem(position);
		pianoViewHolder.getContent().setText(String.valueOf(itemData.getPianoRoomNo()));
		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(convertView.getContext(), android.R.layout.simple_spinner_dropdown_item, itemData.getPianoAvailableTime());
		pianoViewHolder.getTimeSpinner().setAdapter(arrayAdapter);
		pianoViewHolder.getAppointButton().setOnClickListener(view -> {
			PianoRoomAppointment pianoRoomAppointment = new PianoRoomAppointment();
			pianoRoomAppointment.setPianoRoomId(itemData.getPianoRoomId().toString());
			pianoRoomAppointment.setAppointmentTime(pianoViewHolder.getTimeSpinner().getSelectedItem().toString());
			boolean b = pianoRoomServiceImpl.appointmentPianoRoom(pianoRoomAppointment);
			if(b){
				Toast.makeText(view.getContext(), "预约成功", Toast.LENGTH_SHORT).show();
				NavigationUtils.redirectToAppointmentRecordsActivity(view.getContext(), null);
			}else {
				Toast.makeText(view.getContext(), "预约失败，请重试", Toast.LENGTH_SHORT).show();
				updateData(pianoRoomServiceImpl.page(null).getRecords());
			}
		});
		return convertView;
	}

	@Override
	public void updateData(List<PianoRoom> newData) {
		pianoRooms.clear();
		pianoRooms.addAll(newData);
		notifyDataSetChanged();
	}

}

class PianoViewHolder {
	private TextView content;
	private Button appointButton;
	private Spinner timeSpinner;

	public Spinner getTimeSpinner() {
		return timeSpinner;
	}

	public void setTimeSpinner(Spinner timeSpinner) {
		this.timeSpinner = timeSpinner;
	}

	public Button getAppointButton() {
		return appointButton;
	}

	public void setAppointButton(Button appointButton) {
		this.appointButton = appointButton;
	}

	public TextView getContent() {
		return content;
	}

	public void setContent(TextView content) {
		this.content = content;
	}
}
